<!-- 搜索项目 -->
<template>
  <h-select-input-popup
    ref="hSelectInputPopup"
    v-model="val"
    v-bind="$attrs"
    v-on="$listeners"
    :params="queryParams"
    :keywords.sync="keywords"
    :getList="getList"
    @getDetail="getDetail"
		:isMulti="multi"
		:isJudge="true"
		@onJudge="onJudge"
    @change="$emit('change', $event)"
		searchPlaceholder="请输入物料名称"
  >
    <template v-slot:default="{ item }">
      <view class="project-item">
        <view class="option">
          <view class="lf">物料编码</view>
          <view class="rl">{{ item.materialBill }}</view>
        </view>
        <view class="option">
          <view class="lf">物料名称</view>
          <view class="rl">{{ item.materialName }}</view>
        </view>
        <view class="option">
          <view class="lf">品牌</view>
          <view class="rl">{{ item.brand }}</view>
        </view>
        <view class="option">
          <view class="lf">规格型号</view>
          <view class="rl">{{ item.specification }}</view>
        </view>
        <view class="option">
          <view class="lf">单位</view>
          <view class="rl">{{ item.unit_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">图片</view>
          <view class="rl" style="color: #2979ff;" @click.stop="onPicture(item.attachment)">查看图片</view>
        </view>
        <view class="option">
          <view class="lf">物料类别</view>
          <view class="rl">{{ item.provisionBroad_dictText + '-' + item.provisionSmall_dictText }}</view>
        </view>
        <view class="option">
          <view class="lf">参考价格(元)</view>
          <view class="rl">{{ item.referencePrice }}</view>
        </view>
      </view>
    </template>
  </h-select-input-popup>
</template>

<script>
import { erpPurchaseMaterialList } from '@/api/index.js'
import { erpPurchaseMaterialQueryById, erpPurchaseMaterialQueryByIds } from '@/api/index.js'
import {
		BASE_URL
	} from '@/utils/baseUrl.js';
export default {
  name: 'HSelectFinanceInvoiceBillInputPopup',
  data() {
    return {
			BASE_URL,
      getList: erpPurchaseMaterialList,
      params: {
        column: 'createTime',
        order: 'desc',
        pageNo: 1,
        pageSize: 10,
      },
      keywords: '',
    }
  },
  model: {
    prop: 'value',
    event: 'input',
  },
  props: {
    value: {
      type: [String, Number, Array],
      default: null,
    },
    materialFlag: {
      type: Boolean,
      default: false,
    },
	
	multi: {
		type: Boolean,
		default: false,
	},
    provisionSmall: {
      type: String,
      default: '',
    },
    auditStatus: {
      type: String,
      default: '',
    },
    muitFlag: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    val: {
      get() {
        return this.value
      },
      set() {
        this.$emit('input', this.value)
      },
    },
    queryParams() {
      const params = {
        ...this.params,
      }
      if (this.keywords) {
        params.materialName = this.keywords
      }
      if (this.provisionSmall) {
        params.provisionSmall = this.provisionSmall
      }
      if (this.auditStatus) {
        params.auditStatus = this.auditStatus
      }
      return params
    },
  },
  methods: {
    // 获取详情
    getDetail(value) {
		var empIdArr = value.split(',')
		if (empIdArr.length == 1) {
			erpPurchaseMaterialQueryById({ id: value }).then((res) => {
			  if (res.success) {
			    this.$refs.hSelectInputPopup.title = res.result.materialName
			    this.$refs.hSelectInputPopup.checkItem = [res.result]
			    this.$emit('select', {
			      data: res.result,
			      id: value,
			    })
			  }
			})
		} else {
			erpPurchaseMaterialQueryByIds({ids: value}).then(res => {
				if (res.success) {
					this.$emit('select', {
					  data: res.result,
					  id: value,
					})
					this.$refs.hSelectInputPopup.checkItem = res.result
					var title = ''
					res.result.map(value => {
						title = value.materialName + ',' + title
					})
					title = title.substring(0, title.lastIndexOf(','))
					this.$refs.hSelectInputPopup.title = title
				}
			})
		}
    },
		
		onJudge () {
			if (this.provisionSmall) {
				this.$refs.hSelectInputPopup.showPopup = true
			} else {
				uni.showToast({
					title: '请先选择物料小类',
					icon: 'none'
				})
			}
		},
		
		onPicture (item) {
			if (!item) {
				uni.showToast({
					title: '暂无图片',
					icon: 'none'
				})
				return false
			}
			var urlArr = item.split(',')
			urlArr = urlArr.map(item => `${this.BASE_URL}/api/erpmng/sys/common/static/${item}`)
			uni.previewImage({
				urls: urlArr,
				current: 0
			})
		},
  },
}
</script>

<style lang="scss" scoped>

// 按钮
.btn {
  position: fixed;
  bottom: 20rpx;
  width: 95%;
}
</style>
